package com.yunfei.schedule;
import com.yunfei.bo.ScheduleLogBo;
import com.yunfei.contants.ApplicationConstant;
import com.yunfei.contants.RedisConstants;
import com.yunfei.mapper.aiiidb.ScheduleLogMapper;
import com.yunfei.mapper.sqlserver.WorkPlanMapper;
import com.yunfei.service.RedisService;
import com.yunfei.utils.ComponentFactory;
import com.yunfei.utils.DateUtil;
import com.yunfei.utils.id.IdUtils;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Component
public class ScheduledTask {
    private static final org.slf4j.Logger Logger = LoggerFactory.getLogger(ScheduledTask.class);
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
    /**
     * 每隔5秒执行, 单位：ms。
     */
   @Scheduled(fixedRate = 1000)
    public void testMyBatis() {
        try {
            RedisService redisService = ComponentFactory.getBean(RedisService.class);
            Environment env = ComponentFactory.getBean(Environment.class);
            WorkPlanMapper workPlanMapper = ComponentFactory.getBean(WorkPlanMapper.class);
            ScheduleLogMapper scheduleLogMapper = ComponentFactory.getBean(ScheduleLogMapper.class);
            Map<String,Object> queryMap=new HashMap<>();
            int count=workPlanMapper.count(queryMap);
            String today=DateUtil.getToDay();
            if(count==0&&!redisService.exists(today)){
                redisService.set(today,today, RedisConstants.SESSION_EXPIRES_HOUR_24, TimeUnit.HOURS);
                System.out.println("数据被修改了。。。。。。。。。。。。"+DateUtil.getDateNow());
                ScheduleLogBo scheduleLogBo=new ScheduleLogBo();
                scheduleLogBo.setId(IdUtils.getId());
                scheduleLogBo.setType("数据被修改了");
                scheduleLogMapper.add(scheduleLogBo);
                //数据被修改后，重新把数据更改回来
                workPlanMapper.updateOri(queryMap);
                workPlanMapper.updateChiDao(queryMap);
                workPlanMapper.updateChiDao(queryMap);
            }else{
                //System.out.println("数据未被改动");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

        }
    }
}

